//
//  ViewController.m
//  RefreshDemo
//
//  Created by Clavis on 2019/1/2.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "ViewController.h"
#import "UIScrollView+SEGSwindmill.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) CAShapeLayer *circleLayer;
@property (nonatomic, strong) UIView *circleView;
//@property (nonatomic, assign) NSInteger dataSource;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.tableView.backgroundColor = UIColor.lightGrayColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.dataSource = [NSMutableArray array];
    [self.dataSource addObject:@"fjsdkfkjsd金卡戴珊垃圾分类卡机是分开了敬爱时空裂缝"];
    [self.dataSource addObject:@"第三节课绿付付付付发绿卡军所多多多多多多多付金卡戴珊垃圾分类卡机是分开了敬爱时空裂缝"];
    [self.dataSource addObject:@"fjsdkfkjsd金卡戴珊垃圾分类卡机是分开了敬爱时空裂缝"];
    
    [self.tableView addWindmillInSuperView:self.view];
//    [self.tableView justShowfooter];
    
    self.tableView.estimatedRowHeight = 80.0;
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 30, 0);
    
//    [self.tableView addWindmillWithHeaderRefreshBlock:^{
//        NSLog(@"header refresh");
//        [self loadData:YES];
//    }];
//
//    [self.tableView addWindmillWithFooterRefreshBlock:^{
//        NSLog(@"footer refresh");
//         [self loadData:NO];
//    }];
    
    
    UIView *circleView = [[UIView alloc] init];
    circleView.frame = CGRectMake(100, 30, 40, 40);
    self.circleView = circleView;
    self.circleView.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:0.6];
    [self.view addSubview:circleView];

    CAShapeLayer *circleLayer = [[CAShapeLayer alloc] init];
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:20 startAngle:(-0.5*M_PI) endAngle:1.5*M_PI clockwise:YES];
    circleLayer.path = path.CGPath;
    circleLayer.strokeColor = UIColor.orangeColor.CGColor;
    circleLayer.fillColor = UIColor.clearColor.CGColor;
    [circleView.layer addSublayer:circleLayer];
    self.circleLayer = circleLayer;
    
//    self.tableView addWindmillWithFooterRefreshBlock:<#^(void)footerRefreshBlock#>
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
//    self.circleLayer.strokeStart = 0.1;
    
    
//    self.circleLayer.anchorPoint = CGPointMake(1, 1);
//    self.circleLayer.strokeStart = 0.1;
//    CABasicAnimation *animatioin = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//    animatioin.fromValue = @(0.0);
//    animatioin.toValue = @(M_PI * 2);
//    animatioin.duration = 1;
//    animatioin.autoreverses = NO;
//    animatioin.fillMode = kCAFillModeForwards;
//    animatioin.repeatCount = MAXFLOAT;
//    [self.circleLayer addAnimation:animatioin forKey:@"rotate"];
    
    
    
//    CAKeyframeAnimation *path=[CAKeyframeAnimation animationWithKeyPath:@"position"];
//    //矩形的中心就是圆心
//    CGRect rect=CGRectMake(0, 0, 20, 20);
//    path.duration=1;
//    //绕此圆中心转
//    path.path=CFAutorelease(CGPathCreateWithEllipseInRect(rect, NULL));
//    path.calculationMode=kCAAnimationPaced;
//    path.rotationMode=kCAAnimationRotateAuto;
//    path.autoreverses = NO;
//    path.fillMode = kCAFillModeForwards;
//    path.repeatCount = MAXFLOAT;
//    [self.circleLayer addAnimation:path forKey:@"round"];

    
//    NSLog(@"header %@  superView = %@", self.tableView.seg_header, self.tableView.seg_header.superview);
//    self.tableView.seg_header.hidden = NO;
//    NSLog(@"footer %@  superView = %@", self.tableView.seg_footer, self.tableView.seg_footer.superview);
//
    
//    self.tableView.seg_footer.isAutoRefresh = !self.tableView.seg_footer.isAutoRefresh;
//    if (self.tableView.seg_footer.isAutoRefresh) {
//        NSLog(@"隐藏 footer");
//    } else {
//        NSLog(@"显示 footer");
//    }
    
//    self.dataSource = 0;
//    [self.tableView reloadData];
//
//
//    [self.tableView startHeaderAnimatingWindmill];
    
//    CGPoint point = CGPointMake(0, 0);
//    [self scrollViewWillEndDragging:self.tableView withVelocity:CGPointZero targetContentOffset:&point];
    
    
//    NSDate *date = [NSDate date];
//
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        NSDate *endTime =  [NSDate date];
//
//        NSTimeInterval interval = endTime.timeIntervalSinceNow - date.timeIntervalSinceNow;
//        NSLog(@"%lf", interval);
//    });
    
    

    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [scrollView draggingScrollViewInOffsety:scrollView.contentOffset.y];
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
 
    [scrollView startAnimatingWindmill:^(BOOL canHeaderFresh, BOOL isFooter) {
        if (isFooter) {//上拉加载
            if (canHeaderFresh) {//可刷新
                [self loadData:NO];
            }
        }else{//下拉刷新
            if (canHeaderFresh) {//可刷新
                [self loadData:YES];
            }
        }
    }];
}


- (void)loadData:(BOOL)isrefresh {
    //
//    NSEC_PER_SEC
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (isrefresh) {
            
            [self.tableView stopAnimatingWindmill];
        } else {
            
            [self.tableView stopFooterAnimatingWindmill];
        }
        
//        if (isrefresh) {
//            for (NSInteger i = 0; i < 4; <#increment#>) {
//                <#statements#>
//            }
//            self.dataSource = 4;
//        } else {
//            self.dataSource += 6;
//        }
        [self.tableView reloadData];
        
//        if (!isrefresh) {
//            
//            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
//        }
        
        

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView stopAnimatingWindmill];
        });
    });
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
        UILabel *label = [cell.contentView viewWithTag:100];
        label.text = @"会计法扩所付军奥绿所军付绿卡所军付扩绿军发扩绿所付军军军军军军军军军军军军军扣积分卡拉斯加的福利卡建设路口分离焦虑卡视角风口浪尖啊";
        return cell;
    }
    
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //cell.backgroundColor = UIColor.blueColor;
    //cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    UILabel *label = [cell.contentView viewWithTag:101];
    label.text = self.dataSource[indexPath.row];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
